<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        prop="createTime"
        label="提交时间"
        width="200px"
        align="center"
      />
      <el-table-column
        prop="sname"
        label="门店"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        label="成本类型"
        align="center"
        width="100"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{ row }">
          {{ row.sort | sortFilter }}
        </template>
      </el-table-column>
      <el-table-column
        prop="type"
        label="补充说明"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        prop="money"
        label="金额（元）"
        align="center"
        width="160px"
      />
      <el-table-column label="操作" align="center" width="110">
        <template slot-scope="{ row }">
          <el-button
            v-if="row.status === 0"
            type="primary"
            size="mini"
            @click="approve(row)"
          >
            确认
          </el-button>
          <el-tag
            v-else-if="row.status === 1"
            type="warning"
          >财务已确认</el-tag>
          <el-tag v-else type="success">老板已确认</el-tag>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="fetchData"
    />
  </div>
</template>

<script>
import { getSpend, changeSpend } from '@/api/txs'
import Pagination from '@/components/Pagination'

export default {
  components: { Pagination },
  filters: {
    sortFilter (sort) {
      switch (sort) {
        case 0:
          return '房租'
        case 1:
          return '物业'
        case 2:
          return '水电'
        case 3:
          return '宽带'
        case 4:
          return '管家团队'
        case 5:
          return '操作团队'
        case 6:
          return '其他团队'
        case 7:
          return '耗材药品'
        case 8:
          return '三方合作'
        case 9:
          return '流量费用'
        case 10:
          return '客情支出'
        case 11:
          return '运营杂费'
        default:
          return '未知'
      }
    }
  },
  data () {
    return {
      total: 0,
      listQuery: {
        page: 1,
        limit: 13
      },
      spend: {},
      list: null,
      listLoading: true
    }
  },
  created () {
    this.fetchData()
  },
  methods: {
    approve (row) {
      this.$confirm('再次确认, 是否继续?', '提示', {
        confirmButtonText: '确人',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.spend = row
          this.spend.status = 1
          changeSpend(this.spend).then(res => {
            this.spend = {}
            row.status = 1
            this.$notify({
              title: res.data.message,
              message: res.data.status,
              type: 'success',
              duration: 2000
            })
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '未确认'
          })
        })
    },
    fetchData () {
      this.listLoading = true
      getSpend(this.listQuery.page, this.listQuery.limit).then(res => {
        this.list = res.data.data.records
        this.total = res.data.data.total
        this.listLoading = false
      })
    }
  }
}
</script>
